package cn.feichanghao.nutz.quickstart;

import org.nutz.dao.Dao;
import org.nutz.dao.tools.Tables;
import org.nutz.ioc.Ioc;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

import cn.feichanghao.nutz.quickstart.auth.model.Menu;
import cn.feichanghao.nutz.quickstart.auth.model.User;
import cn.feichanghao.nutz.quickstart.service.MenuService;
import cn.feichanghao.nutz.quickstart.service.UserService;

public class QuickStartSetup implements Setup {

	private static final Log log = Logs.getLog(QuickStartSetup.class);

	public void init(NutConfig config) {
		if (log.isDebugEnabled())
			log.debug("auth module constructor");

		Ioc ioc = config.getIoc();
		Dao dao = ioc.get(Dao.class, "dao");
		if (!dao.exists(User.class)) {
			// POJO-->DB
			Tables.define(dao, Tables.parse(User.class));
			// Create Default admin user
			UserService userService=ioc.get(UserService.class,"userService");
			userService.create("admin", "admin", "default admin");
			
			for (int i = 0; i < 100; i++)
			{
				userService.create("username"+i, "password"+i, "desc"+i);
				log.debugf("u:%s,n:%s", "username"+i,"password"+i);
			}
			log.debugf("当前有%s个用户", userService.count());
		}
		
		if(!dao.exists(Menu.class))
		{
			Tables.define(dao, Tables.parse(Menu.class));
			MenuService menuService=new MenuService();
			for (int i = 0; i < 6; i++) {
				menuService.create("菜单名称"+i,"catalog?id="+i,"菜单介绍");
			}
		}
	}
	
	

	public void destroy(NutConfig config) {}
}
